//
//  HomeViewController.m
//  MateNet
//
//  Created by ky on 12-11-18.
//  Copyright (c) 2012年 ky. All rights reserved.
//

#import "HomeViewController.h"
#import "LoginViewController.h"
#import "UIWeiboCell.h"

@interface WeiboUnit : NSObject
{
    NSString* m_strName;
    NSString* m_strTime;
    NSString* m_strProfileName;
    NSString* m_strText;
}

@property(retain) NSString* m_strName;

@property(retain) NSString* m_strTime;

@property(retain) NSString* m_strProfileName;

@property(retain) NSString* m_strText;

@end

@implementation WeiboUnit

@synthesize m_strName;

@synthesize m_strText;

@synthesize m_strProfileName;

@synthesize m_strTime;

@end

//------------------------------------------

//------------------------------------------

@implementation HomeViewController

@synthesize m_tableView;

@synthesize m_receiveData;

@synthesize m_arrayData;

//-----------------------------------------
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)StartDownloadData
{
    NSString* strFriendsWeibo = [NSString stringWithFormat: @"https://api.weibo.com/2/friendships/friends.json?access_token=%@&uid=%@&trim_status=0", [LoginViewController GetToken], [LoginViewController GetUid]];
    
    NSURLRequest* request = [NSURLRequest requestWithURL: [NSURL URLWithString:strFriendsWeibo]];
    
    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if (connection == nil)
    {
        NSLog(@"connect error, failed to download weibo data");
    }
    else
    {
        m_receiveData = [[NSMutableData alloc] init];
        
        m_arrayData = [[NSMutableArray alloc] init];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [m_receiveData setLength:0];
}

-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    [m_receiveData appendData:data];
}

-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError *)error
{
    [m_receiveData setLength:0];
}

const NSString* Name = @"name";

const NSString* ProfileImage = @"profile_image_url";

const NSString* Text = @"text";

const NSString* Status = @"status";

const NSString* Users = @"users";

const NSString* Time = @"created_at";

-(NSString*)GetWeibo:(NSDictionary*)dic and:(NSString*)key
{
    if (dic == nil)
    {
        return @"";
    }
    
    return [dic objectForKey:key];
}

-(void)UpdateArrayData
{
    NSJSONSerialization* jsonData = [NSJSONSerialization JSONObjectWithData:m_receiveData options:NSJSONReadingMutableLeaves error:nil];
        
    if (jsonData == nil)
    {
        return;
    }

    NSMutableArray* arrayData = [jsonData valueForKey: Users];
    
    for (NSDictionary* cell in arrayData)
    {
        WeiboUnit* xWeiboUnit = [[WeiboUnit alloc] init];
        
        xWeiboUnit.m_strName = [cell objectForKey: Name];
        
        xWeiboUnit.m_strProfileName = [cell objectForKey: ProfileImage];
        
        xWeiboUnit.m_strText = [self GetWeibo: [cell objectForKey: Status] and: Text];
        
        xWeiboUnit.m_strTime = [self GetWeibo: [cell objectForKey: Status] and: Time];
            
        [m_arrayData addObject: xWeiboUnit];
    }
    
    [self.m_tableView reloadData];
}

-(void)SetTextForCell:(UIWeiboCell*)cell with:(WeiboUnit*)xWeiboUnit
{  
     cell.m_LabelName.text = xWeiboUnit.m_strName;
    
    cell.m_TextWeibo.text = xWeiboUnit.m_strText;
    
    cell.m_LabelTime.text = xWeiboUnit.m_strTime;
    
    cell.m_LabelFrom.text = @"iphone";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* strSimpleTableIndentifier = @"SimpleTableItem";
    
    UIWeiboCell* cell = (UIWeiboCell*)[tableView dequeueReusableCellWithIdentifier:strSimpleTableIndentifier];
    
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        
        cell = [nib objectAtIndex:0];
    }
    
    WeiboUnit* xWeiboUnit = [m_arrayData objectAtIndex:indexPath.row];
    
    [self SetTextForCell:cell with:xWeiboUnit];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_arrayData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return 237; 
}

-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    [self UpdateArrayData];
    
    [m_receiveData release];
    
    m_receiveData = nil;
    
    [connection release];
    
    [self.m_tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.

    [self StartDownloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
